<?php

class edit_champclasses
{
	var $champid, $mess = '';

	function edit_champclasses($champid)
	{
		global $skin_dir, $db, $lang;
		$this->champid = $champid;
		if(isset($_POST['remove']) && isset($_POST['classID']))
		{
			$remQ = 'delete from '. CHAMP_CLASSES_TABLE .' where c_class ='. $_POST['classID'] . ' AND c_champid = ' . $this->champid;
			$result = $db->sql_query($remQ);
			$selQ = 'select cl_title FROM ' . CLASSES_TABLE . ' where cl_id = ' . $_POST['classID'];
			$result = $db->sql_query($selQ);
			if($row = $db->sql_fetchrow($result)) 
			{
				$this->mess = new addMessage(str_replace('{entry}', $row['cl_title'], $lang['AdminRemoved']),	$lang['AdminRemovedTitle']);
			}
			else
			{
				$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
			}
		}
		else if(isset($_POST['add']) && isset($_POST['classID']) && is_numeric($_POST['classID']) && 
				isset($_POST['divisionID']) && is_numeric($_POST['divisionID']))
		{
			$insQ = 'insert into '. CHAMP_CLASSES_TABLE .' values ('. $_POST['classID'] .', ' . $this->champid . ', ' . $_POST['divisionID'] . ')';
			$result = $db->sql_query($insQ);
			$selQ = 'select cl_title FROM ' . CLASSES_TABLE . ' where cl_id = ' . $_POST['classID'];
			$result = $db->sql_query($selQ);
			if($row = $db->sql_fetchrow($result)) 
			{
				$this->mess = new addMessage(str_replace('{entry}', $row['cl_title'], $lang['AdminAdded']),	$lang['AdminAddedTitle']);
			}
		}
	}

	function render()
	{
		
		if($this->mess != '')
		{
			$this->mess->render();
		}
		$this->displayEditor();
		
	}
	
	function displayEditor()
	{
		global $skin_dir, $db, $lang;
		
		$currentClasses = '';
		$nonClasses = '';
		$vehicles = '';
		$divisions = '<option value="0">' . $lang['ChampionshipDivDefault'] . '</option>';
		
		//two sql statements
		$q_none = 'SELECT cl_title, cl_id 
				   FROM ' . CLASSES_TABLE . ', ' . CHAMPIONSHIPS_TABLE . '
				   WHERE c_id = '. $this->champid . ' AND cl_game = c_game AND cl_id NOT IN
				   		(SELECT c_class FROM ' . CHAMP_CLASSES_TABLE . ' WHERE c_champid = ' . $this->champid . ')

				   ORDER BY cl_title ASC';

					
		$q_div = 'SELECT d_id, d_title 
				  FROM ' . DIVISIONS_TABLE . ' 
				  WHERE d_champid = ' . $this->champid . '
				  ORDER BY d_order ASC';
				  
		$q_current = 'SELECT cl_title, cl_id, d_title
		              FROM ' . CLASSES_TABLE . ', ' . CHAMP_CLASSES_TABLE . '
					  LEFT JOIN ' . DIVISIONS_TABLE .' 
					  ON c_defaultdiv = d_id
					  WHERE cl_id = c_class AND c_champid = ' . $this->champid . '
					  ORDER BY cl_title ASC';
					  
		$q_vehicles = 'SELECT c_model, c_manufacturer 
					   FROM ' . CARS_TABLE . ' 
					   WHERE c_class IN ( ' .
    						'SELECT cl_id FROM ' . CLASSES_TABLE . ' WHERE cl_id IN 
									(SELECT c_class FROM ' . CHAMP_CLASSES_TABLE . ' WHERE c_champid = ' . $this->champid . ')
							 ORDER BY cl_title ASC) 
					   ORDER BY c_manufacturer';			
					   
		$result = $db->sql_query($q_none);
		while($row = $db->sql_fetchrow($result)) {
			$nonClasses = $nonClasses . '<option value="' . $row['cl_id'] . '">' . $row['cl_title'] . '</option>';
		}
		
		$result = $db->sql_query($q_current);
		while($row = $db->sql_fetchrow($result)) {
			$currentClasses = $currentClasses . '<option value="' . $row['cl_id'] . '">' . $row['cl_title'] . 
								($row['d_title'] != '' ? ' (' . $row['d_title'] . ')' :  ' (' . $lang['ChampionshipDivDefault'] . ')' ) . 
								'</option>';
		}
		
		$result = $db->sql_query($q_vehicles);
		while($row = $db->sql_fetchrow($result)) {
			$vehicles = $vehicles . $row['c_manufacturer'] . ' ' . $row['c_model'] . '<br>';
		}
		
		$result = $db->sql_query($q_div);
		while($row = $db->sql_fetchrow($result)) {
			$divisions = $divisions . '<option value="' . $row['d_id'] . '">' . $row['d_title'] . '</option>';
		}
		
		include $skin_dir . 'admin/editchampionshipclass.tpl';
	}

}

?>